/*
 * This file is part of the Refran Library
 * Copyright (C) 2009 César Enrique García
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 * $Author: cquike $
 * $Date: 2010-10-07 14:37:47 +0000 (Thu, 07 Oct 2010) $
 * $Revision: 218 $
 */

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE RecipeLoggerSuite
#define _GLIBCXX_USE_NANOSLEEP
#include <iostream>
#include <vector>
#include <thread>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include "Recipe.h"
#include "Logger.h"
#include "HeavyLoggerRecipe.h"
#include "Error.h"
#include "log4cpp/FileAppender.hh"


BOOST_AUTO_TEST_SUITE(RecipeLoggerSuite)

/**
 * @test
 */
class MySubsystem : public Refran::Logger
{
    public:

    MySubsystem() : Refran::Logger("OtherSubsystem")
    {
    }

    void logging()
    {
        for(int i = 0; i < 1000; i++)
            warning_("Useless message %d",i);
    }
};

BOOST_AUTO_TEST_CASE(loggingMultithread)
{
    int nExec = 10;
    const int nThreads = 10;
    log4cpp::FileAppender fileAppender("FileAppender","LoggingFile.log");

    MySubsystem system;
    system.setAppender(fileAppender);
    for(int i = 0; i< nExec; ++i)
    {
        HeavyLoggerRecipe recipes[nThreads];
        std::shared_ptr<Refran::FrameSet> frameset;
        for(int i = 0; i < nThreads; ++i)
        {
            recipes[i].setInputFrames(frameset);
            recipes[i].setAppender(fileAppender);
        }
        boost::thread_group pool;
        for(int j = 0; j< nThreads; ++j)
        {
            pool.create_thread
                (boost::bind(&HeavyLoggerRecipe::go, &(recipes[j])));
            pool.create_thread
                (boost::bind(&MySubsystem::logging, &system));
        }
        pool.join_all();
    }
    BOOST_REQUIRE_NO_THROW(boost::filesystem::remove("LoggingFile.log"));
}

BOOST_AUTO_TEST_SUITE_END()
